<?php
namespace Home\Controller;
use Think\Controller;
class AboutController extends Controller {
    public function index(){
        $uid=session('uid');
        $type=M('type')->select();
        $this->type=$type;
        if(!$uid)
        {$this->error('登录已过期!',U("Home/Login/index"));}
        elseif($type){
            $this->assign('list', $type);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Home/About/index"));}
    }
    public function detail(){
        $tid = $_GET['tid'];
        $newsmodel=M('news');
        $news=$newsmodel->where(array("tid" => $tid,"satus" =>"1"))->join('users on users.uid=news.n_uid')->select();
        /*dump($news);*/
        for ($i = 0; $i < count($news); $i++){
            $news[$i]["code"] = $i + 1;
            if($news[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($news[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($news[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($news[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($news[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($news[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($news[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $news[$i]["tid"]=$type[$i];
        }

        if($news){
            $this->assign('list', $news);
            $this->display();
        }
        else{ $this->error('暂时没有该类别新闻，请重新选择', U("Home/About/index"));}

    }
    public function detaillist(){
        $id = $_GET['id'];
        $newsmodel=M('news');
        $news=$newsmodel->where(array("id" => $id))->join('users on users.uid=news.n_uid')->select();


        for ($i = 0; $i < count($news); $i++){

            if($news[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($news[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($news[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($news[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($news[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($news[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($news[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $news[$i]["tid"]=$type[$i];
        }



        if($news){
            $this->assign('list', $news);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Home/About/index"));}
    }


    public function collections(){
        $uid=session("uid");
        $id = $_GET['id'];
        $collections = M("collections");
        $a = $collections-> where(array('c_uid'=>$uid,'id'=>$id)) -> find();
        if($a){
        $this->error('本新闻您已收藏!',U("Home/About/index"));
        }
        elseif ($collections) {
            $collections->id = $id;
            $collections->c_uid = $uid;
            $collections->add();
            $this->success('收藏成功');
        } else {
            $this->error('收藏失败!');

        }

    }

    public function comments(){
        $uid=session('uid');
        $title=$_POST['title'];
        $id=$_GET['id'];
        $data['uid'] = $uid;
        $data['title'] = $title;
        $data['id'] = $id;
        $data['content'] = trim(I('post.content'));
        $data['cdate'] = date('y-m-d', time());
        if($data['content']==null){
            $this->error('请输入评论再提交，谢谢亲喽');
        }
        else
        {$result=M('comments')->add($data);}
        if($result){$this->success('评论成功,亲，我们会仔细看的哦~~ >^_^<');}
        else{ $this->error('评论失败!');}
    }

    public function fabulous(){
        $uid=session('uid');
        $id = $_GET['id'];
        $add=M('fabulous')->where(array("fs_id"=>$id))->select();
        if($add==null){
            $data['fs_id']=$id;
            $data['fabulous']=1;
            $data['fs_uid']=$uid;
            M("fabulous")->add($data);
        }
        //$on=M('fabulous')->where(array("fs_id"=>$id))->select();

      else{
          $edit=M('fabulous')->where(array("fs_id"=>$id))->select();
          $addone["fabulous"]=$edit[0]["fabulous"]+1;
         M('fabulous')->where(array("fs_id"=>$id))->save($addone);
          }

        if($uid){
            $this->success('亲，点赞成功哦，谢谢您对本新闻的支持 >^_^<');
        }
        else{$this->error('亲，本系统暂时有误，请重新点赞，谢谢');}
    }
}